﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ccit.OA.Utils;

namespace Ccit.OA.NewsCenter
{
    public partial class news_column_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
               // if(_hidTabId.Value == "")
                _hidTabId.Value =( null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("NEWS_A2",_hidTabId.Value);
                //修改父节点主键
                hdColumnId.Value = (null != Request.QueryString["columnId"]) ? Request.QueryString["columnId"].ToString() : "";
                //信息类型
                hdType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";
                //操作类型
                hdOper.Value = (null != Request.QueryString["oper"]) ? Request.QueryString["oper"].ToString() : "";
                _hidOriUpTypeId.Value = "0";
            }
            initializePage();
        }
        private void initializePage()
        {
            
            ColumnBLL bll = new ColumnBLL();

            string sRangeColIds = bll.GetRangeColumn(_hidWhereClause.Value);
            if (sRangeColIds != "" && hdColumnId.Value != "")
            {
                if (!sRangeColIds.Contains(hdColumnId.Value))
                {
                    btnSave.Disabled = false;
                    btnSaveAdd.Disabled = false;
                    tab1.Disabled = false;
                }
            }

            List<MsColumn> mclist = new List<MsColumn>();
            mclist = bll.GetDropDownList(hdType.Value,hdColumnId.Value);            
            this.ddlColumn.DataSource = mclist;
            this.ddlColumn.DataTextField = "columnName";
            this.ddlColumn.DataValueField = "columnId";            
            this.ddlColumn.DataBind();
            ddlColumn.Items.Insert(0, "(无上级类别)");
            ddlColumn.SelectedIndex = 0;

            rbtnPubType.SelectedIndex = 0;
            //修改
            if (hdOper.Value== "1")
            {
                initializeEdit(bll);
            }
            //新增
            if (hdOper.Value == "0")
            {
                initializeAddNew(bll);
            }
        }
        //新增
        private void initializeAddNew(ColumnBLL bll)
        {
            if (hdColumnId.Value == "")
            {
                ddlColumn.SelectedIndex = 0;
            }
            else
            {
                ddlColumn.SelectedValue = hdColumnId.Value;
            }
            string columnId = hdColumnId.Value;
            _hidOriUpTypeId.Value = columnId;
            
            txtColumnName.Text = "";
            rbtnPubType.SelectedIndex = 0;
            txtPubPurview.Text = "";
            txtBrowsePurview.Text = "";
            txtManagePurview.Text = "";
            txtOrderNo.Text = "";
            txtColumnDesc.Value = "";
            hdColumnId.Value ="";
        }
        //修改
        private void initializeEdit(ColumnBLL bll)
        {
            _hidOriUpTypeId.Value = hdColumnId.Value;
            MsColumn mc = bll.GetMsColumnObj(hdColumnId.Value);
            txtColumnName.Text = mc.columnName;
            rbtnPubType.SelectedIndex = Convert.ToInt32(mc.pubType);
            //发布，浏览，管理权限
            hdPubId.Value = mc.pubPurviewId;
            hdPubType.Value = mc.pubPurviewType;
            hdBrowseId.Value = mc.browsePurview;
            hdBrowseType.Value = mc.browsePurviewType;
            hdManageId.Value = mc.managePurview;
            hdManageType.Value = mc.managePurviewType;

            txtPubPurview.Text = SysInterface.GetNameStrByIdStr(mc.pubPurviewId, mc.pubPurviewType, " ");
            txtManagePurview.Text = SysInterface.GetNameStrByIdStr(mc.managePurview, mc.managePurviewType, " ");
            txtBrowsePurview.Text = SysInterface.GetNameStrByIdStr(mc.browsePurview, mc.browsePurviewType, " ");
            
                          
            txtOrderNo.Text = mc.orderNo;
            txtColumnDesc.Value = mc.columnDesc;
            hdColumnId.Value = mc.columnId;


            this.ddlColumn.SelectedValue = bll.GetFatherObj(hdColumnId.Value).columnId;

            // get up column id 获取修改前上级父节点
            MsColumn msUpCol = bll.GetFatherObj(hdColumnId.Value);
            _hidOriUpTypeId.Value = (null != msUpCol && !string.IsNullOrEmpty(msUpCol.columnId)) ?
                msUpCol.columnId : "";

            msUpCol = null;
        }

        protected void lnbCallback_Click(object sender, EventArgs e)
        {
             
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveColumn(string jsonExpr, string operType)
        {
            MsColumn mc = (MsColumn)JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsColumn));
            ColumnBLL bll = new ColumnBLL();
            List<string> slist = bll.InsertColumn(mc);
            var oResult = new { resType = slist[0], resMes = slist[1], resOperType = operType, resColumnId = slist[2],resUpTypeId=slist[3],hasChildSign = slist[4] };
            return JavaScriptConvert.SerializeObject(oResult);
        }
    }
}
